﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using TeachMe.Application.Data;

namespace TeachMe.Application
{
    [Serializable]
    public class ClipboardEntries
    {
        public const string CustomFormatName = "TEACH_ME_ENTRIES";

        static ClipboardEntries()
        {
            DataFormats.GetFormat(CustomFormatName);
        }

        public bool IsEmpty
        {
            get
            {
                return Items == null || Items.Count == 0;
            }
        }

        public void Copy()
        {
            DataObject dataObject = new DataObject();
            dataObject.SetData(CustomFormatName, this);
            dataObject.SetData(DataFormats.Text, ConvertToText());

            Clipboard.SetDataObject(dataObject);
        }

        [Serializable]
        public class ClipboardEntry
        {
            public string From { get; set; }
            public string To { get; set; }
            public string Example { get; set; }
            public Priority Priority { get; set; }
        }

        public List<ClipboardEntry> Items { get; set; }
        public string From { get; set; }
        public string To { get; set; }

        public string ConvertToText()
        {
            StringBuilder builder = new StringBuilder();

            foreach (ClipboardEntry data in Items)
            {
                if (!String.IsNullOrEmpty(data.To))
                {
                    builder.Append(data.To);
                }
                builder.Append(";");
                if (!String.IsNullOrEmpty(data.From))
                {
                    builder.Append(data.From);
                }
                builder.Append(";");
                if (!String.IsNullOrEmpty(data.Example))
                {
                    builder.Append(data.Example);
                }
                builder.AppendLine();
            }

            return builder.ToString();
        }
    }
}